package com.fulin.sycnLife;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 一个简单的缓冲例子
 * Cache组合一个非线程安全的HashMap作为缓存的实现，同时使用读写锁的读锁和写锁来保证Cache是线程安全的。
 * @author fulin16
 *
 */
public class Cache {
	static Map<String,Object> map = new HashMap<String,Object>();
	static ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
	static Lock r = rwl.readLock();
	static Lock w = rwl.writeLock();
	//获取一个key对应的value
	public static final Object get(String key){
		r.lock();
		try{
			return map.get(key);
		}finally{
			r.unlock();
		}
	}
	
	//设置key对应的value,并返回旧的value
	public static final Object put(String key,Object value){
		w.lock();
		try{
			return map.put(key, value);
		}finally{
			w.unlock();
		}
	}
	
	//清空所有的内容
	public static final void clear(){
		w.lock();
		try{
			map.clear();
		}finally{
			w.unlock();
		}
	}
}
